package com.itlong.cloud.controller.finance;

import com.itlong.cloud.POJO.DTO.operate.OperateOurPayInfoDTO;
import com.itlong.cloud.POJO.DTO.operate.OperateThirdPayInfoDTO;
import com.itlong.cloud.POJO.DTO.operate.OperateThirdPayPageDTO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.annotation.AuthAccessRequired;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.constants.LogDescConstant;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.enumerate.ecode.OperateErrorCodeEnum;
import com.itlong.cloud.paymenthub.IOperateThirdPayServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;

/**
 * <desc>
 * 运营-财务管理模块-第三方支付管理主控制器.
 * </desc>
 *
 * @createDate 2018/9/25
 */
@RestController
@RequestMapping("/finance")
public class OperateThirdPayController {

    private static final Logger LOG = LoggerFactory.getLogger(OperateThirdPayController.class);

    @Autowired
    IOperateThirdPayServiceClient iOperateThirdPayServiceClient;

    /**
     * <desc>
     *      第三方支付信息分页查询
     * <desc/>
     *
     * @param operateThirdPayPageDTO 分页查询参数
     * @return
     * @author Qiang.S
     * @createDate 2018-09-25
     */
    @ResponseBody
    @AuthAccessRequired
    @RequestMapping(path = "/thirdPay/getThirdPayInfoPage", method = RequestMethod.POST)
    public Object getThirdPayInfoPage(OperateThirdPayPageDTO operateThirdPayPageDTO){
        MessageVO messageVO;
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            operateThirdPayPageDTO= HttpProtocolUtil.parseRequestParamToDTO(OperateThirdPayPageDTO.class,request);
            //请求对象不能为空
            if(null == operateThirdPayPageDTO){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            messageVO = new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),iOperateThirdPayServiceClient.getThirdPayInfoPage(operateThirdPayPageDTO));
        }catch (Exception e){
            throw new DataAccessException("【运营_财务管理_第三方支付信息分页查询getThirdPayInfoPage】第三方支付信息分页查询失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      添加
     * <desc/>
     *
     * @param operateThirdPayInfoDTO 第三方提现信息
     * @return
     * @author Qiang.S
     * @createDate 2018-09-20
     */
    @ResponseBody
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.THIRDPAY_INSERT_LOG_DESC)
    @RequestMapping(path = "/thirdPay/saveThirdPayInfo", method = RequestMethod.POST)
    public Object saveThirdPayInfo(OperateThirdPayInfoDTO operateThirdPayInfoDTO){
        MessageVO messageVO;
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            operateThirdPayInfoDTO= HttpProtocolUtil.parseRequestParamToDTO(OperateThirdPayInfoDTO.class,request);
            //请求对象不能为空
            if(null == operateThirdPayInfoDTO){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            //第三方
            if(StringUtils.isBlank(operateThirdPayInfoDTO.getThirdPartyPayment())){
                return new MessageVO(OperateErrorCodeEnum.THIRDPAY_THIRDPARTYPAYMENT_EMPTY_ERR.getErrorCode());
            }
            //手续费规则
            if(StringUtils.isBlank(operateThirdPayInfoDTO.getFeeRule())){
                return new MessageVO(OperateErrorCodeEnum.THIRDPAY_FEERULE_EMPTY_ERR.getErrorCode());
            }
            //手续费
            if(StringUtils.isBlank(operateThirdPayInfoDTO.getHandlingFee())){
                return new MessageVO(OperateErrorCodeEnum.THIRDPAY_HANDINGFEE_EMPTY_ERR.getErrorCode());
            }
            //功能状态
            if(StringUtils.isBlank(operateThirdPayInfoDTO.getStatus())){
                return new MessageVO(OperateErrorCodeEnum.THIRDPAY_STATUS_EMPTY_ERR.getErrorCode());
            }
            if (operateThirdPayInfoDTO.getPaymentManagementId() == null && iOperateThirdPayServiceClient.getThirdPayCount(operateThirdPayInfoDTO.getThirdPartyPayment()) > 0) {
                return new MessageVO(OperateErrorCodeEnum.THIRDPAY_DATA_REPEAT_ERR.getErrorCode());
            }
            iOperateThirdPayServiceClient.saveThirdPayInfo(operateThirdPayInfoDTO);
            messageVO = new MessageVO(operateThirdPayInfoDTO.getThirdPartyPayment(),BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【运营_财务管理添加saveThirdPayInfo】添加失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      物业提现规则设置
     * <desc/>
     *
     * @param operateOurPayInfoDTO 公司提现规则
     * @return
     * @author Qiang.S
     * @createDate 2018-09-20
     */
    @ResponseBody
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.OURPAYINFO_INSERT_LOG_DESC)
    @RequestMapping(path = "/thirdPay/saveOurPayInfo", method = RequestMethod.POST)
    public Object saveOurPayInfo(OperateOurPayInfoDTO operateOurPayInfoDTO){
        MessageVO messageVO;
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            operateOurPayInfoDTO= HttpProtocolUtil.parseRequestParamToDTO(OperateOurPayInfoDTO.class,request);
            //请求对象不能为空
            if(null == operateOurPayInfoDTO){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            //收款手续费
            if(StringUtils.isBlank(operateOurPayInfoDTO.getTakeHandlingFee())){
                return new MessageVO(OperateErrorCodeEnum.THIRDPAY_TAKEHANDLINGFEE_EMPTY_ERR.getErrorCode());
            }
            //付款手续费
            if(StringUtils.isBlank(operateOurPayInfoDTO.getPayHandlingFee())){
                return new MessageVO(OperateErrorCodeEnum.THIRDPAY_PAYHANDLINGFEE_EMPTY_ERR.getErrorCode());
            }
            //自动提现最小金额
            if(StringUtils.isBlank(operateOurPayInfoDTO.getLowAmount())){
                return new MessageVO(OperateErrorCodeEnum.THIRDPAY_LOWAMOUNT_EMPTY_ERR.getErrorCode());
            }
            if(new Double(operateOurPayInfoDTO.getLowAmount()) < 2000){
                return new MessageVO(OperateErrorCodeEnum.THIRDPAY_LOWAMOUNT_TOO_LOW_ERR.getErrorCode());
            }
            iOperateThirdPayServiceClient.saveOurPayInfo(operateOurPayInfoDTO);
            messageVO = new MessageVO(operateOurPayInfoDTO.getPayHandlingFee()+"|"+
                    operateOurPayInfoDTO.getTakeHandlingFee()+"|"+operateOurPayInfoDTO.getLowAmount(),BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【运营_财务管理_物业提现规则设置saveOurPayInfo】物业提现规则设置失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      查询第三方支付详细信息
     * <desc/>
     *
     * @param paymentManagementId 支付管理ID 逻辑ID
     * @return
     * @author Qiang.S
     * @createDate 2018-09-25
     */
    @ResponseBody
    @RequestMapping(path = "/thirdPay/getThirdPayDetail", method = RequestMethod.POST)
    public Object getThirdPayDetail(String paymentManagementId){
        MessageVO messageVO;
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            paymentManagementId= request.getAttribute("paymentManagementId")==null?null:request.getAttribute("paymentManagementId").toString();
            //请求对象不能为空
            if(StringUtils.isBlank(paymentManagementId)){
                return new MessageVO(OperateErrorCodeEnum.THIRDPAY_PAYMENTMANAGEMENTID_EMPTY_ERR.getErrorCode());
            }
            messageVO = new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),iOperateThirdPayServiceClient.getThirdPayDetail(paymentManagementId));
        }catch (Exception e){
            throw new DataAccessException("【运营_财务管理_查询第三方支付详细信息getThirdPayDetail】查询第三方支付详细信息失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      获取物业提现规则
     * <desc/>
     *
     * @return
     * @author Qiang.S
     * @createDate 2018-09-25
     */
    @ResponseBody
    @RequestMapping(path = "/thirdPay/getOurPayInfo", method = RequestMethod.POST)
    public Object getOurPayInfo(){
        MessageVO messageVO;
        try {
            messageVO = new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),iOperateThirdPayServiceClient.getOurPayInfo());
        }catch (Exception e){
            throw new DataAccessException("【运营_财务管理_获取物业提现规则getOurPayInfo】获取物业提现规则信息失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      修改状态
     * <desc/>
     *
     * @param paymentManagementId 支付管理ID
     * @param status 支付管理ID  1正常 2禁用
     * @return
     * @author Qiang.S
     * @createDate 2018-09-20
     */
    @ResponseBody
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.THIRDPAY_UPDATE_STATUS_LOG_DESC)
    @RequestMapping(path = "/thirdPay/updateStatus", method = RequestMethod.POST)
    public Object updateStatus(String paymentManagementId,String status){
        MessageVO messageVO;
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            paymentManagementId= request.getAttribute("paymentManagementId")==null?null:request.getAttribute("paymentManagementId").toString();
            status= request.getAttribute("status")==null?null:request.getAttribute("status").toString();
            //第三方
            if(StringUtils.isBlank(paymentManagementId)){
                return new MessageVO(OperateErrorCodeEnum.THIRDPAY_PAYMENTMANAGEMENTID_EMPTY_ERR.getErrorCode());
            }
            //手续费规则
            if(StringUtils.isBlank(status)){
                return new MessageVO(OperateErrorCodeEnum.THIRDPAY_STATUS_EMPTY_ERR.getErrorCode());
            }
            iOperateThirdPayServiceClient.updateStatus(paymentManagementId,status);
            messageVO = new MessageVO(paymentManagementId+"|"+status,BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【运营_财务管理添加saveThirdPayInfo】添加失败",e);
        }
        return messageVO;
    }

}